﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using Jzy.Workflow.Line;
using Jzy.Workflow.Node;
using System.Text;

using Jzy.Workflow.Line.Rules;

using System.Xml.Linq;
using Frame.Lib.Web;
using Frame.Lib.Enums;
using Frame.Lib.Utilities;
using Frame.Lib.Core.Dept;
using Frame.Lib.Core.Role;
using Frame.Lib.Common;


namespace Jzy.Web.WorkFlow.Design.Line
{
    public partial class Edit : System.Web.UI.Page
    {
        private string FlowID
        {
            get
            {
                return Request["FlowID"];
            }
        }
        protected string PKID
        {
            get
            {
                return Request["PKID"];
            }
        }
        private FlowLineBO _flowLine = new FlowLineBO();
        private FlowNodeBO _flowNode = new FlowNodeBO();

        protected void Page_Load(object sender, EventArgs e)
        {
            CssFileManager.AddCssFile(CssFile.tab2);
            BindList();
            if (!IsPostBack)
            {
                BindFromNode();
                BindToNode();
                WebUtil.BindListControlWithEnum(TypeID, typeof(LineType));

                DispInfo();
              
                this.Page.Form.DefaultButton = btnSave.UniqueID;
            }

            AddAttr();
        }

        private void AddAttr()
        {
            JSUtil.ShowDlg(hidRoles, txtRoles, "../Node/SelectRole.aspx");
            JSUtil.ShowTreeDlg(hidDepts, txtDepts, "Dept", "选择部门", false);            
        }

       

        private void DispInfo()
        {
            if (PKID != null)
            {
                FlowLineInfo info = _flowLine.GetInfo(PKID);
                EntityUtil.SetControl(pnlMain, info);

                if (!string.IsNullOrEmpty(info.Rules))
                {
                    XElement rules = XElement.Parse(info.Rules);
                    hidDepts.Value = rules.Element("Depts").Value;
                    hidRoles.Value = rules.Element("Roles").Value;
                    if (hidDepts.Value != "")
                    {
                        DeptBO dept = new DeptBO();
                        txtDepts.Text = StrUtil.MergeToStr(dept.GetName(StrUtil.SplitToInt(hidDepts.Value)), ",");
                    }
                    if (hidRoles.Value != "")
                    {
                        RoleBO role = new RoleBO();
                        txtRoles.Text = StrUtil.MergeToStr(role.GetName(StrUtil.SplitToInt(hidRoles.Value)), ",");
                    }                    

                    IEnumerable<XElement> fields = rules.Elements("Field");

                    if (fields != null && fields.Count() > 0)
                    {
                        StringBuilder script = new StringBuilder("var custRules = [");
                        foreach (XElement field in fields)
                        {
                            script.Append("{FieldName:'" + field.Element("FieldName").Value + "',Relation:'" + field.Element("Relation").Value + "',CompareValue:'" + field.Element("CompareValue").Value + "',DataType:'" + field.Element("DataType").Value + "',Logic:'" + field.Element("Logic").Value + "'},");

                        }
                        string js = script.ToString().TrimEnd(',') + "];initRules(custRules);";
                        JSUtil.ExcuteJS(js);
                    }
                    //FieldName.Text = field.Element("FieldName").Value;
                    //Relation.SelectedValue = field.Element("Relation").Value;
                    //CompareValue.Text = field.Element("CompareValue").Value;
                    //DataType.SelectedValue = field.Element("DataType").Value;                    

                }
            }
        }
        private void BindFromNode()
        {
            string flowID = this.FlowID;
            if (flowID == null)
            {
                flowID = _flowLine.GetInfo(PKID).FlowID.ToString();
            }

            DataTable dt = _flowNode.GetDataTable(int.Parse(flowID));
            DataView dv = dt.DefaultView;
            dv.RowFilter = "TypeID <>" + (int)FlowNodeType.结束;
            BindDrp(FromNodeID, dv);
        }

        private void BindToNode()
        {
            string flowID = this.FlowID;
            if (flowID == null)
            {
                flowID = _flowLine.GetInfo(PKID).FlowID.ToString();
            }

            DataTable dt = _flowNode.GetDataTable(int.Parse(flowID));
            DataView dv = dt.DefaultView;

            dv.RowFilter = "TypeID <>" + (int)FlowNodeType.拟稿 + " and ID <> " + FromNodeID.SelectedValue;
            BindDrp(ToNodeID, dv);
        }

       
        private void BindDrp(DropDownList drp, DataView dv)
        {
            drp.DataSource = dv;
            drp.DataTextField = "Name";
            drp.DataValueField = "ID";
            drp.DataBind();
        }

        private void BindList()
        {
            List<EnumInfo> list = ConvertUtil.ConvertEnum(typeof(LineTab));
            rpt.DataSource = list;
            rpt.DataBind();

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            FlowLineInfo info = _flowLine.GetInfo(PKID);
            EntityUtil.SetEntity(pnlMain, info);
            if (PKID == null)
            {
                info.FlowID = int.Parse(FlowID);                
            }

            XElement rules = new XElement("Rules");
            XElement ele = new XElement("Roles");
            ele.Value = hidRoles.Value;
            rules.Add(ele);

            ele = new XElement("Depts");
            ele.Value = hidDepts.Value;
            rules.Add(ele);


            if (hidRules.Value != "")
            {
                string[] custRules = hidRules.Value.Split('|');
                foreach (string str in custRules)
                {
                    XElement field = new XElement("Field");

                    string[] fields = str.Split(',');                   

                    ele = new XElement("FieldName");
                    ele.Value = fields[0];
                    field.Add(ele);

                    ele = new XElement("Relation");
                    ele.Value = fields[1];
                    field.Add(ele);

                    ele = new XElement("CompareValue");
                    ele.Value = fields[2];
                    field.Add(ele);

                    ele = new XElement("DataType");
                    ele.Value = fields[3];
                    field.Add(ele);

                    ele = new XElement("Logic");
                    ele.Value = fields[4];
                    field.Add(ele);

                    rules.Add(field);
                }
               
            } 
            info.Rules = rules.ToString();


            if (_flowLine.Exist(info.FromNodeID, info.ToNodeID, PKID))
            {
                JSUtil.Alert("该路径已存在");
            }
            else
            {
                _flowLine.Save(info);
                JSUtil.RefreshView();
                //StringBuilder sbJS = new StringBuilder();
                //sbJS.Append("<script language='javascript'>\n");
                //sbJS.Append("returnValue=true;\n");
                //sbJS.Append("window.close();\n");
                //sbJS.Append("</script>");

                //Response.Write(sbJS.ToString());
                //Response.End();
            }
        }

        protected void FromNodeID_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindToNode();
        }
    }
}
